package com.wujw.netty.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Desc:
 *
 * @author wujw
 * @email jiwenwu@outlook.com
 * @date 2018/11/13
 */
public class BIOServer {
    // 1.server端服务启动
    // 2.server端服务器阻塞监听client请求
    // 3.server端服务器接收请求，创建线程实现任务

    private static final Integer PORT = 8888;

    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;

        try {
            serverSocket = new ServerSocket(PORT);
            while (true) {
                // 服务器监听：阻塞、等待client请求
                socket = serverSocket.accept();
                System.out.println("服务器接收请求：" + socket);
                // 服务器连接确认：确认client请求后，创建线程执行任务
                new Thread(new BIOServerHandler(socket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                if (socket != null){
                    socket.close();
                }
                if (serverSocket != null){
                    serverSocket.close();
                    System.out.println("BIO server 服务器关闭");
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

}
